//
//  ListTableViewCell.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/6/25.
//

import UIKit

class ListTableViewCell: UITableViewCell {

    private var cellImage: UIImageView!
    private var cellTitle : UILabel!
    private var cellTime : UILabel!
    private var cellProgressLabel : UILabel!
    private var progressView : UIProgressView!
    
    private var cellShiyan : UILabel!
    
    var model: BookModel? {
        didSet {
            if let imageStr = model?.imageStr {
                if let typeStr = model?.imageType {
                    if Int(typeStr) == 1 {
                        if let savedImage = String.convertStrToImage(imageStr) {
                            cellImage.image = savedImage
                        }
                    } else {
                        cellImage.image = UIImage(named: imageStr)
                    }
                }
            }
            cellTitle.text = "\(model?.book_title ?? "") - \(model?.book_author ?? "")"
            cellShiyan.text = model?.book_shiyan
            guard let addTime = model?.addTime else {return}
            guard let timeInterval = Double(addTime) else {return}
            let date = Date.init(timeIntervalSince1970: timeInterval)
             
            let dateFormatte = DateFormatter()
            dateFormatte.dateFormat = "yyyy-MM-dd"
            cellTime.text = dateFormatte.string(from: date)
            
            cellProgressLabel.text = "已阅读0%"
            progressView.setProgress(0.0, animated: false)
            if let progress = model?.progress {
                let proFloat = Float(progress) ?? 0.0
                cellProgressLabel.text = "已阅读\(Int(proFloat*100))%"
                progressView.setProgress(proFloat, animated: true)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        cellImage = UIImageView()
        cellImage.layer.cornerRadius = 5
        cellImage.layer.masksToBounds = true
        cellImage.contentMode = .scaleAspectFill
        self.addSubview(cellImage)
        cellImage.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(20)
            make.height.equalTo(100)
            make.width.equalTo(cellImage.snp.height).multipliedBy(640.0/749.0)
        }
        
        cellTitle = UILabel()
        cellTitle.font = UIFont.systemFont(ofSize: 14)
        cellTitle.textColor = kColor333333
        cellTitle.numberOfLines = 2
        self.addSubview(cellTitle)
        cellTitle.snp.makeConstraints { make in
            make.left.equalTo(cellImage.snp.right).offset(20)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(cellImage).offset(5)
        }
        
        cellShiyan = UILabel()
        cellShiyan.font = UIFont.systemFont(ofSize: 14)
        cellShiyan.textColor = kColor666666
        cellShiyan.numberOfLines = 2
        self.addSubview(cellShiyan)
        cellShiyan.snp.makeConstraints { make in
            make.left.right.equalTo(cellTitle)
            make.top.equalTo(cellTitle.snp.bottom).offset(10)
        }
        
        cellTime = UILabel()
        cellTime.font = UIFont.systemFont(ofSize: 12)
        cellTime.textColor = kColor666666
        self.addSubview(cellTime)
        cellTime.snp.makeConstraints { make in
            make.right.equalTo(cellTitle)
            make.bottom.equalTo(cellImage)
        }
        
        cellProgressLabel = UILabel()
        cellProgressLabel.font = UIFont.systemFont(ofSize: 12)
        cellProgressLabel.textColor = kColor666666
        cellProgressLabel.text = "已阅读0%"
        self.addSubview(cellProgressLabel)
        cellProgressLabel.snp.makeConstraints { make in
            make.right.equalTo(cellTitle)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        progressView = UIProgressView(progressViewStyle: .default)
        progressView.progressTintColor = kThemeColor
        progressView.trackTintColor = kLineColor
        progressView.progress = 0.0
        self.addSubview(progressView)
        progressView.snp.makeConstraints { make in
            make.left.equalTo(cellImage)
            make.right.equalTo(cellProgressLabel.snp.left).offset(-10)
            make.centerY.equalTo(cellProgressLabel)
            make.height.equalTo(10)
        }
        
        let cellLine = UILabel()
        cellLine.backgroundColor = kLineColor
        self.addSubview(cellLine)
        cellLine.snp.makeConstraints { make in
            make.left.equalTo(cellImage)
            make.bottom.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
